//
//  StayViewController.m
//  cityStar
//
//  Created by joyban on 14-12-25.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "StayViewController.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"
#import "UIColor+FlatUI.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"
#import "RJPhotoViewController.h"
#import "RJPhoto.h"
//商品列表头文件
#import "PersonnelListViewController.h"
#import "webView.h"
//cell头文件
#import "CommunityDetailCell.h"
#import "CommunityDetailTwoCell.h"
#import "shopDetailCell.h"
#import "CollectionCell.h"
#import "StarRatingCell.h"

NSString *const StayTableViewCellID = @"StayCellID";
NSString *const StayTwoTableViewCellID = @"StayTwoCellID";
NSString *const StayShopDetailCellID = @"stayShopDetailCellID";
NSString *const CollectionStayTableViewCellID = @"collectionStayCellID";
NSString *const StarRatingStayTableViewCellID = @"starRatingStayCellID";

@interface StayViewController ()<CSbtnDelegate,RJPhotoViewControllerDataSource,RJPhotoViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *StayTableView;
@property (strong,nonatomic) IBOutlet UIView *heardTableView;
@property (weak,nonatomic) NSString *status;
@property (nonatomic, weak) IBOutlet UIButton *btnBuyOne;
@property (nonatomic, weak) IBOutlet UIButton *btnBuyMore;

@end

@implementation StayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navtitle = @"预定住宿类";
    
    self.btnBuyOne.layer.cornerRadius = 4;
    self.btnBuyOne.clipsToBounds = YES;
    
    self.btnBuyMore.layer.cornerRadius = 4;
    self.btnBuyMore.clipsToBounds = YES;
    //通过上个界面来返回标题
    self.navigationItem.title = self.navtitle;
    [self addBackButton];
    self.status = @"YES";
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithImage:[UIImage imageNamed:@"collection_n"] actionBlock:^{
        weakSelf.status = @"YES";
        [weakSelf selectBtn];
    }];
    [self setUpTableViews];
    [self setHeardTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)selectBtn
{
    if([self.status isEqualToString:@"YES"])
    {
        [SVProgressHUD showSuccessWithStatus:@"收藏成功"];
        __weak typeof(self)weakSelf = self;
        [self addRightButtonWithImage:[UIImage imageNamed:@"collection_y"] actionBlock:^{
            weakSelf.status = @"NO";
            [weakSelf selectBtn];
        }];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"取消收藏"];
        __weak typeof(self)weakSelf = self;
        [self addRightButtonWithImage:[UIImage imageNamed:@"collection_n"] actionBlock:^{
            weakSelf.status = @"YES";
            [weakSelf selectBtn];
        }];
    }
}

#pragma mark - collectionCell btndelegate
- (IBAction)productbtn_Action:(UIButton *)sender {
    PersonnelListViewController *personnelListViewController = [[PersonnelListViewController alloc] init];
    [self.navigationController pushViewController:personnelListViewController animated:true];
}

-(void)senderBtnClick:(UIButton *)btn
{
    PersonnelListViewController *personnelListViewController = [[PersonnelListViewController alloc] init];
    [self.navigationController pushViewController:personnelListViewController animated:true];
}

- (IBAction)PackageBtn_Action:(UIButton *)sender {
    PersonnelListViewController *personnelListViewController = [[PersonnelListViewController alloc] init];
    [self.navigationController pushViewController:personnelListViewController animated:true];
}

#pragma mark - 轮转

-(void)setHeardTableView
{
    self.StayTableView.tableHeaderView = self.heardTableView;
}

-(IBAction)btnPhoto_Action:(id)sender
{
    RJPhotoViewController *photoVC = [[RJPhotoViewController alloc] initWithDataSource:self delegate:self];
    photoVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:photoVC animated:YES];
}

#pragma mark - RJPhotoView DataSource
- (NSInteger)photoViewNumberOfPhoto
{
    //    return [self.currentMoment.imgList count];
    return 5;
}

- (RJPhoto *)photoView:(RJPhotoViewController *)photoView photoForRow:(NSUInteger)row
{
    RJPhoto *photo = [[RJPhoto alloc] init];
    //    MomentImg *momentImg = self.currentMoment.imgList[row];
    photo.URL = [NSURL URLWithString:@"http://classimage.b0.upaiyun.com/201412/jre5hnts.png"];
    
    photo.title = @"hello world";
    photo.caption = @"zxznzxncjkjncskjdcnskjdnckjdsnkjccszx";
    return photo;
}

#pragma mark - RJPhotoView Delegate
- (NSInteger)numberOfCommentCount
{
    //    return [self.currentMoment.commCount integerValue];
    return 5;
}

- (void)photoViewDidSelectCommentButton
{
    //    [self _goToCommentListVCWithMoment:self.currentMoment];
}

- (void)_goToCommentListVCWithMoment:(Moment *)moment
{
    //    CommentListVC *commentListVC = [[CommentListVC alloc] initWithMoment:moment];
    //    [self.navigationController pushViewController:commentListVC animated:YES];
}




-(void)setUpTableViews
{
    [self.StayTableView registerNib:[CommunityDetailCell nib] forCellReuseIdentifier:StayTableViewCellID];
    [self.StayTableView registerNib:[CommunityDetailTwoCell nib] forCellReuseIdentifier:StayTwoTableViewCellID];
    [self.StayTableView registerNib:[shopDetailCell nib] forCellReuseIdentifier:StayShopDetailCellID];
    [self.StayTableView registerNib:[CollectionCell nib] forCellReuseIdentifier:CollectionStayTableViewCellID];
    [self.StayTableView registerNib:[StarRatingCell nib] forCellReuseIdentifier:StarRatingStayTableViewCellID];
    
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return [CommunityDetailCell heightOfTableCell];
    }else if(indexPath.section == 1)
    {
        return [CommunityDetailTwoCell heightOfTableCell];
    }else if(indexPath.section == 2)
    {
        return [StarRatingCell heightOfTableCell];
    }else if(indexPath.section == 3)
    {
        return [shopDetailCell heightOfTableCell];
    }else{
        return [CollectionCell heightOfTableCell];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0 || section == 3)
    {
        return 0;
    }else{
        return 30;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 1)
    {
        return 3;
    }else{
        return 1;
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel * label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(15, 5, 200, 20);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorFromHexCode:@"333333"];
    label.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"FFF3E2"]];
    [sectionView addSubview:label];
    
    if(section==1)
    {
        label.text = @"基本信息";
    }else if(section == 2)
    {
        label.text = @"星级评分";
    }else if(section == 4)
    {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 5, 20, 20)];
        [imageView setImage:[UIImage imageNamed:@"recom"]];
        [sectionView addSubview:imageView];
        [label setFrame:CGRectMake(45, 5, 200, 20)];
        label.textColor = [UIColor colorFromHexCode:@"949494"];
        label.text = @"到过这里的人还看过";
    }
    
    return sectionView;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        CommunityDetailCell *staydetailCell = [tableView dequeueReusableCellWithIdentifier:StayTableViewCellID];
        staydetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return staydetailCell;
        
    }else if(indexPath.section == 1)
    {
        CommunityDetailTwoCell *staydetailTwoCell = [tableView dequeueReusableCellWithIdentifier:StayTwoTableViewCellID];
        staydetailTwoCell.selectionStyle = UITableViewCellSelectionStyleNone;
        //根据每行修改数据
        if(indexPath.row == 0)
        {
            [staydetailTwoCell.heardImage setImage:[UIImage imageNamed:@"range"]];
            staydetailTwoCell.contentLabel.text = @"您的位置";
        }
        if(indexPath.row == 1)
        {
            [staydetailTwoCell.heardImage setImage:[UIImage imageNamed:@"list_tel"]];
            staydetailTwoCell.contentLabel.text = @"167298238923";
        }
        
        return staydetailTwoCell;
        
    }
    else if(indexPath.section == 2)
    {
        StarRatingCell *starRatingstayCell = [tableView dequeueReusableCellWithIdentifier:StarRatingStayTableViewCellID];
        starRatingstayCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return starRatingstayCell;
        
    }else if(indexPath.section == 3)
    {
        shopDetailCell *ShopDetailCell = [tableView dequeueReusableCellWithIdentifier:StayShopDetailCellID];
        ShopDetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
        return ShopDetailCell;
        
    }else{
        CollectionCell *collectionstayCell = [tableView dequeueReusableCellWithIdentifier:CollectionStayTableViewCellID];
        collectionstayCell.delegate = self;
        collectionstayCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return collectionstayCell;
        
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1)
    {
        if(indexPath.row == 1)
        {
            RIButtonItem *leftButton = [RIButtonItem itemWithLabel:@"确定" action:^{
                NSString *telStr=[@"tel://" stringByAppendingString:@"167298238923"];
                NSURL *url=[NSURL URLWithString:telStr];
                [[UIApplication sharedApplication]openURL:url];
            }];
            RIButtonItem *rightButton = [RIButtonItem itemWithLabel:@"取消"];
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否拨打该号码？" message:@"167298238923" cancelButtonItem:rightButton otherButtonItems:leftButton, nil];
            [alertView show];
        }
    }
    if(indexPath.section == 3)
    {
        webView *webViewController = [[webView alloc]init];
        webViewController.navTitle = @"商铺名称";
        [self.navigationController pushViewController:webViewController animated:YES];
    }
    [tableView reloadData];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
